/**
 * 
 * @param makeOpt : 데이터를 가져올 방법. 'xml' , 'select' , 'list'
 * @param objId   : 'select' , 'list' 일때 데이터를 뿌려줄 각 엘리먼트의 id
 * @param divSelector : 위 objId 로 만들어진 결과물(셀렉트박스 혹은 <UL>리스트)이 포함될 <DIV>
 * @param elemFunc : 
 * @param params : 
 * @param objClass :
 * @returns
 */
getCategoryInfo = function(makeOpt , objId , divSelector , elemFunc , params , objClass) {
	var url = '/carcenter/data/category_list.gcm' + (makeOpt=='json'? '?dataType=json' : '?dataType=xml');
	return getAjaxXmlInfo(url , params , makeOpt , objId , divSelector , elemFunc , params , objClass);
};

getCarcInfo = function(makeOpt , objId , divSelector , elemFunc , params , objClass) {
	var url = '/carcenter/data/carc_list.gcm' + (makeOpt=='json'? '?dataType=json' : '?dataType=xml');
	return getAjaxXmlInfo(url , params , makeOpt , objId , divSelector , elemFunc , params , objClass);
};

getEvalInfo = function(makeOpt , objId , divSelector , elemFunc , params , objClass) {
	var url = '/carcenter/data/eval_list.gcm' + (makeOpt=='json'? '?dataType=json' : '?dataType=xml');
	return getAjaxXmlInfo(url , params , makeOpt , objId , divSelector , elemFunc , params , objClass);
};

getCarcUserInfo = function(makeOpt , objId , divSelector , elemFunc , params , objClass) {
	var url = '/carcenter/data/carc_user_list.gcm';
	return getAjaxXmlInfo(url , params , makeOpt , objId , divSelector , elemFunc ,  objClass);
};

getCarcInfoAround = function(makeOpt , params , objId , divSelector , elemFunc , objClass) {
	var url = '/carcenter/data/carc_list_around.gcm' + (makeOpt=='json'? '?dataType=json' : '');
	return getAjaxXmlInfo(url , params , makeOpt , objId , divSelector , elemFunc , objClass);
};

getAjaxXmlInfo = function(url , params , makeOpt , objId , divSelector , elemFunc ,  objClass) {
	var returnStr = '';
	$.ajax({
		url: url,
		data: params,
		type: 'POST',
		async: false,
		dataType: makeOpt == 'json'? 'html' : 'xml',
		success : function(xml){
			switch(makeOpt) {
			case 'select' :
				makeSelectByXml(xml , objId , divSelector , elemFunc , objClass);
				break;
			case 'list' :
				makeListByXml(xml , objId , divSelector , elemFunc , objClass);
				break;
			case 'xml' :
				returnStr = xml;
				break;
			case 'json' :
				returnStr = xml;
				break;
			default :
				break;
			};
		
		}
	});
	
	return returnStr;
};

makeSelectByXml = function(xml , selectId , targetDiv , onSelectChange , selectClass){
	var $select = $("<select>");
	var $input , $cursor;
	$select.attr('id' , selectId);
	$select.attr('name' , selectId);
	if(selectClass) $select.addClass(selectClass);
	$select.attr('multiple' , 'multiple');
	$(xml).find('item').each(function(){
		$cursor = $(this);
		$input = $("<option>");
		$input.val( $cursor.find('info_seq').text() );
		$input.text( $cursor.find('name').text() );
		$select.append($input);
	});
	var size = $(xml).find('item').length;
	$select.attr('size' , $(xml).find('item').length );
	$(targetDiv).empty();
	if(size > 0) $(targetDiv).append($select);
	else $(targetDiv).append("<LABEL>none</LABEL>");
};

makeListByXml = function(xml , listId , targetDiv , onListClick ,  listClass) {
	var $ul = $("<ul>");
	$ul.attr('id' , listId);
	$ul.attr('name' , listId);
	
	listClass = 'ulClass';
	if(listClass) $ul.addClass(listClass);
	$(xml).find('item').each(function(){
		var $cursor = $(this);
		var $li = $("<li>");
		$li.text( $cursor.find('name').text() );
		$li.addClass('liClass');
		$li.css('cursor' , 'pointer');
		$li.hover(function(){
			$(this).css('background' , '#8eb5ee');
		} , function(){
			$(this).css('background' , '#ffffff');
		});
		if(onListClick) $li.click( function(){
			onListClick( $cursor );
		} );
		$ul.append($li);
	});
	var size = $(xml).find('item').length;
	$(targetDiv).empty();
	if(size > 0) $(targetDiv).append($ul);
	else $(targetDiv).append("<P>none</P>");
};

viewInfo = function( cur ) {
	var targetDiv = $("#index_carc_info");
	targetDiv.empty();
	if(!cur) return;
	var title = $("<strong>").text('정비소 정보');
	targetDiv.append( title );
	
	commonInfo( cur , targetDiv );
};

viewUserInfo = function( cur , targetDiv ) {
	var targetDiv = $("#index_carc_info");
	targetDiv.empty();
	var title = $("<strong>").text('정비소 정보');
	targetDiv.append( title );
	
	var comment = $("<span>").text(' - 등록 대기중 입니다.').css('color' , '#d83c71').css('font-size' , '11px');
	targetDiv.append( comment );
	commonInfo( cur , targetDiv );

	var insertBtn = $("<input type='button' value='등록' style='margin-left:10px;'>");
	insertBtn.click( function(){
		if(confirm('뭥? 정말 등록할거임?')) {
			$.post( '/carcenter/mov/userInfo.gcm' , {user_info_seq : cur.find('user_info_seq').text()} , function(data){
				console.log(data);
				if(data =='Y') {
					getCarcInfo('list' , 'sel_carc_info' , '#index_carc_list' , viewInfo);
					getCarcUserInfo('list' , 'sel_carc_user_info' , '#index_carc_user_list' , viewUserInfo);
					viewInfo(); // 초기화
				}
				else {
					alert('등록 실패.');
				}
			} );
		}
	} );
	targetDiv.append( insertBtn );
};

commonInfo = function( cur , targetDiv ) {
	var ul = $("<ul>");
	ul.addClass('ulClass');
	var name = $("<li>").html('이름 : ' + cur.find('name').text()).addClass('liClass');
	var addr = $("<li>").html('주소 : ' + cur.find('addr').text()).addClass('liClass');
	var time = $("<li>").html('시간 : ' + cur.find('worktime').text()).addClass('liClass');
	var pic  = $("<li>").html('사진 : <img src="' + cur.find('picture').text() +'" />').addClass('liClass');
	
	var viewMap = $("<li style='margin-top:11px;'>").html('<a href=\'javascript:googleMap('+cur.find('lat').text()+' , '+cur.find('lng').text()+' , "'+cur.find('name').text()+'" , "'+ targetDiv.attr('id') +'");\'>지도에서 보기</a>');
	ul.append(name);
	ul.append(addr);
	ul.append(time);
	ul.append( pic );
	ul.append( viewMap );
	targetDiv.append( ul );
};

googleMap = function(lat , lng , display_name , targetDivId) {
	window.open('/pop_map.jsp?lat=' + lat + '&lng=' + lng + '&carcenter_name=' + display_name , 'pop_map' , ' width= 300px , height= 400px , scrollbars=no , menubar=no , toolbar=no , location=no , status=no , resizable=no');
};

getAddrTree = function() {
	
	var url = '/carcenter/data/getAddrTree.gcm'
		, params 
		, makeOpt = 'json'; 
	return getAjaxXmlInfo(url , params , makeOpt );
}